﻿using DaprPlus.Domain.Events;
using DaprPlus.Extensions;
using DaprPlus.Identity.Domain.Entities;
using DaprPlus.Identity.Domain.IntegrationEvents;
using DaprPlus.Specifications;
using Microsoft.Extensions.DependencyInjection;

namespace DaprPlus.Identity.Domain.Services
{
    public interface IUserSyncService
    {
        Task Sync();
    }


    public class UserSyncService(IServiceProvider provider) : IUserSyncService
    {
        public async Task Sync()
        {
            var repository = provider.GetRepository<User>();
            var trueSpec = new TrueSpecification<User>();
            var dtos = await repository.GetAllAsync<UserRecord>(trueSpec);
            var syncEvent = new UserSyncIntegrationEvent([.. dtos], true);
            var eventBus = provider.GetRequiredService<IIntegrationEventBus>();
            await eventBus.PublishAsync(syncEvent);
        }
    }
}
